//
//  CashHomeViewController.m
//  CashBook
//
//  Created by 吴斌 on 2024/9/3.
//

#import "CashHomeViewController.h"
#import "CashAllDataModel.h"
#import "CashHomeTableCell.h"
#import "CashHomeHeadView.h"
@interface CashHomeViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong)UITableView * cashTableView;
@property (nonatomic, strong)CashHomeHeadView * cashHomeHeadView;
@property (nonatomic, strong)CashAllDataModel * cashAllDataMdl;
@end

@implementation CashHomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"记账";
    self.view.backgroundColor = [UIColor colorWithDisplayP3Red:246/255.0 green:247/255.0 blue:249/255.0 alpha:1.0];
    
    UINavigationBarAppearance *appearance = [[UINavigationBarAppearance alloc] init];
    self.navigationController.navigationBar.standardAppearance = appearance;
    self.navigationController.navigationBar.scrollEdgeAppearance = appearance;
    [self bulidCashSubView];
}
- (void)bulidCashSubView{
    self.cashTableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.cashTableView.delegate = self;
    self.cashTableView.dataSource = self;
    self.cashTableView.backgroundColor = [UIColor clearColor];
    self.cashTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.cashTableView];
    [self.cashTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(@(0));
    }];
    self.cashHomeHeadView = [[CashHomeHeadView alloc] init];
    self.cashTableView.tableHeaderView = self.cashHomeHeadView;
    
}
#pragma mark -UITableViewDelegate,UITableViewDataSource
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView{
    NSInteger sectionCount = 0;
    CashAllYearDataModel *cashAllYearDataMdl = self.cashAllDataMdl.allRecordArray.lastObject;
    CashAllMonthDataModel *cashAllMonthDataMdl = cashAllYearDataMdl.allYearRecord.lastObject;
    for (CashAllDayDataModel *cashAllDayDataMdl in cashAllMonthDataMdl.allMonthRecord) {
        if (cashAllDayDataMdl.allDayRecord.count > 0) {
            sectionCount++;
        }
    }
    return sectionCount;
}
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    CashAllYearDataModel *cashAllYearDataMdl = self.cashAllDataMdl.allRecordArray.lastObject;
    CashAllMonthDataModel *cashAllMonthDataMdl = cashAllYearDataMdl.allYearRecord.lastObject;
    CashAllDayDataModel *cashAllDayDataMdl = cashAllMonthDataMdl.allMonthRecord[section];
    return  cashAllDayDataMdl.allDayRecord.count;
}
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    CashHomeTableCell *cashHomeTableCell = [CashHomeTableCell makeCashHomeTableCell:tableView];
    cashHomeTableCell.cashAllDataMdl = self.cashAllDataMdl;
    return cashHomeTableCell;
}
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return  70;
}
//-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
//    CashHomeHeadView * cashHomeHeadView = [[CashHomeHeadView alloc] init];
//    return cashHomeHeadView;
//}
//-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
//    return 100;
//}
//-(NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
//    ThisDayDataModel *dayTemp = [[self.userdataModel.allRecordArray lastObject].thisYearRecord lastObject].thisMonthAllRecord[section];
//    return  [NSString stringWithFormat: @"%ld日 支出 ¥%.2f 收入 ¥%.2f",dayTemp.recordDayDate, dayTemp.thisDayExpense,dayTemp.thisDayIncome];
//}
//-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//    ThisDayDataModel *dayTemp = [[self.userdataModel.allRecordArray lastObject].thisYearRecord lastObject].thisMonthAllRecord[indexPath.section];
//    RecordModel *temp = dayTemp.thisDayAllRecord[indexPath.row];
//    RecoredEditVC *vc = [[RecoredEditVC alloc] init];
//    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithTitle:@"详情" style:UIBarButtonItemStylePlain target:nil action:nil];
//       [self.navigationItem setBackBarButtonItem:backItem];
//    __weak typeof(self) weakSelf = self;
//    vc.backRecordBlock = ^(RecordModel * _Nonnull record, NSInteger choseTpye) {
//        if (choseTpye == 2) {
//            [weakSelf updateUserData:record];
//        }else{
//            [weakSelf deleteUserData:record];
//
//        }
//        [weakSelf.headerView setUserData:weakSelf.userdataModel];
//        [weakSelf.tableView reloadData];
//    };
//    vc.choseRecord = temp;
//    vc.recordType = temp.recordType;
//    [self.navigationController pushViewController:vc animated:YES];
//    
//}
//- (IBAction)addRecordButn:(UIButton *)sender {
//    RecordMoneyViewController *recordVC = [[RecordMoneyViewController alloc] init];
//    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:nil action:nil];
//       [self.navigationItem setBackBarButtonItem:backItem];
//    __weak typeof(self) weakSelf = self;
//    recordVC.recordEditVC = [[RecoredEditVC alloc] init];
//
//    recordVC.recordEditVC.backRecordBlock = ^(RecordModel * _Nonnull record, NSInteger choseTpye) {
//        [weakSelf saveUserData:record];
//        [weakSelf.headerView setUserData:weakSelf.userdataModel];
//        [weakSelf.tableView reloadData];
//    };
//    [self.navigationController pushViewController:recordVC animated:YES];
//}
//- (IBAction)humanBookBtn:(UIButton *)sender {
//    HumanBooksViewController *humanBooks = [[HumanBooksViewController alloc] init];
//    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithTitle:@"人情账本" style:UIBarButtonItemStylePlain target:nil action:nil];
//       [self.navigationItem setBackBarButtonItem:backItem];
//    [self.navigationController pushViewController:humanBooks animated:YES];
//}
//- (IBAction)mineBtn:(UIButton *)sender {
//    MineViewController *mineVC = [[MineViewController alloc] init];
//    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithTitle:@"我的" style:UIBarButtonItemStylePlain target:nil action:nil];
//       [self.navigationItem setBackBarButtonItem:backItem];
//    mineVC.userdataModel = self.userdataModel;
//    [self.navigationController pushViewController:mineVC animated:YES];
//}
//#pragma mark -添加一条记录后，保存到本地
//- (void) saveUserData:(RecordModel *)record{
//    NSInteger yearsN = [Tool getDate:record.time dateType:@"YYYY"];
//    NSInteger monthN = [Tool getDate:record.time dateType:@"MM"];
//    NSInteger dayN = [Tool getDate:record.time dateType:@"dd"];
//
//    BOOL isYear = NO;
//    for (ThisYearDataModel *year in self.userdataModel.allRecordArray) {
//        if (year.recordYearDate  == yearsN) {
//            isYear = YES;
//            BOOL isMonth = NO;
//            for (ThisMonthDataModel *month in year.thisYearRecord) {
//                if (month.recordMonthDate == monthN ) {
//                    isMonth = YES;
//                    BOOL isDay = NO;
//                    for (ThisDayDataModel *day in month.thisMonthAllRecord) {
//                        if (day.recordDayDate == dayN) {
//                            day.recordDayDate = dayN;
//                            record.ID = [[NSString stringWithFormat:@"%ld%ld%ld00%ld",yearsN,monthN,dayN,day.thisDayAllRecord.count+1] doubleValue];
//
//                            [day.thisDayAllRecord addObject:record];
//                            isDay = YES;
//                            break;
//                        }
//                    }
//                    if (!isDay) {
//                        ThisDayDataModel *dayTemp = [[ThisDayDataModel alloc] init];
//                        record.ID = [[NSString stringWithFormat:@"%ld%ld%ld001",yearsN,monthN,dayN] doubleValue];
//                        [dayTemp.thisDayAllRecord addObject:record];
//                        [month.thisMonthAllRecord addObject:dayTemp];
//                    }
//                }
//            }
//            if (!isMonth) {
//                ThisMonthDataModel *monthTemp = [[ThisMonthDataModel alloc] init];
//                ThisDayDataModel *dayTemp = [[ThisDayDataModel alloc] init];
//                record.ID = [[NSString stringWithFormat:@"%ld%ld%ld001",yearsN,monthN,dayN] doubleValue];
//                [dayTemp.thisDayAllRecord addObject:record];
//                [monthTemp.thisMonthAllRecord addObject:dayTemp];
//                [year.thisYearRecord addObject:monthTemp];
//            }
//        }
//    }
//    if (!isYear) {
//        ThisYearDataModel *yearTemp = [[ThisYearDataModel alloc] init];
//        ThisMonthDataModel *monthTemp = [[ThisMonthDataModel alloc] init];
//        ThisDayDataModel *dayTemp = [[ThisDayDataModel alloc] init];
//        record.ID = [[NSString stringWithFormat:@"%ld%ld%ld001",yearsN,monthN,dayN] doubleValue];
//        [dayTemp.thisDayAllRecord addObject:record];
//        [monthTemp.thisMonthAllRecord addObject:dayTemp];
//        [yearTemp.thisYearRecord addObject:monthTemp];
//        [self.userdataModel.allRecordArray addObject:yearTemp];
//    }
//    //保存数据到本地
//    [Tool saveLocalData:self.userdataModel pathNameString:userRecordPathName];
//   
//}
//#pragma mark -删除一条记录后，保存到本地
//- (void) deleteUserData:(RecordModel *)record{
//    NSInteger yearsN = [Tool getDate:record.time dateType:@"YYYY"];
//    NSInteger monthN = [Tool getDate:record.time dateType:@"MM"];
//    NSInteger dayN = [Tool getDate:record.time dateType:@"dd"];
//    for (ThisYearDataModel *year in self.userdataModel.allRecordArray) {
//        if (year.recordYearDate  == yearsN) {
//            for (ThisMonthDataModel *month in year.thisYearRecord) {
//                if (month.recordMonthDate == monthN ) {
//                    for (ThisDayDataModel *day in month.thisMonthAllRecord) {
//                        if (day.recordDayDate == dayN) {
//                            for (RecordModel *recordTemp in day.thisDayAllRecord) {
//                                if (recordTemp.ID == record.ID) {
//                                    [day.thisDayAllRecord removeObject:recordTemp];
//                                    break;
//                                }
//                            }
//                        }
//                    }
//                }
//            }
//        }
//    }
//    //保存数据到本地
//    [Tool saveLocalData:self.userdataModel pathNameString:userRecordPathName];
//}

@end
